package com.dog.account.service.impl;

import com.dog.account.mapper.AccountMapper;
import com.dog.account.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author 郭鲁政
 */
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Override
    @Transactional
    public void decrease(Long userId, BigDecimal money) throws Exception {
        BigDecimal residue = accountMapper.selectByUserIdAfter(userId).getResidue();
        if (residue.compareTo(money)<0){
            throw new Exception("可用余额不足");
        }
        accountMapper.decrease(userId, money);
    }
}
